package com.my.test.ctrl;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.AbstractRefreshableApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.my.test.service.TestService;

@Controller
public class MyCtrl {

	@Autowired
	private TestService testService;
	
	@RequestMapping("/test1")
	@ResponseBody
	public String test1(HttpServletRequest request){
		testService.get();
		return "ok";
	}
	
	@RequestMapping("/test")
	@ResponseBody
	public String test(HttpServletRequest request){
		WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();    
        ServletContext servletContext = webApplicationContext.getServletContext(); 
//        servletContext.ref
		WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
		if (context.getParent() !=null) {
			((AbstractRefreshableApplicationContext) context.getParent()) .refresh(); 
		}
		((AbstractRefreshableApplicationContext) context).refresh();
		return "ok";
	}
}
